﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using com.fukefu.source;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.update.instantiated.MarketShare
{
    public class PM_URL : PM
    {
        Preferences pre = new Preferences();
        Queue<WebUrl> queue_url = new Queue<WebUrl>();
        Source source = new Source();

        string reg_title = "<title.*?>(.*?)</title>";

        public override void Init()
        {
            Display("------更新网址------");
            Display("初始化中...");
            Display("更新中...");
            LoadURL();
            new MultiThread().Begin(new Preferences().GetInt("网页线程"), new ThreadStart(UpdateURL));
            Display("网址更新结束...");
        }
        //加载所有网址
        private void LoadURL()
        {
            SqlDataReader re = sql.Query("select url,uid,times from [_pm_网址] where times>-1 and times<3 order by newid()");
            while (re.Read())
            {
                queue_url.Enqueue(new WebUrl(re["uid"].ToString(), re["url"].ToString()));
            }
            re.Close();
            SetMaxNum(queue_url.Count);
        }
        //更新网址
        private void UpdateURL()
        {
            Display(Thread.CurrentThread.Name + " 启动...");
            WebUrl site = null;
            do
            {
                lock (queue_url)
                {
                    if (queue_url.Count == 0)
                    {
                        break;
                    }
                    site = queue_url.Dequeue();
                }
                ProcessUrl(site);
            } while (true);
            Display(Thread.CurrentThread.Name + " 结束...");
        }
        //处理一个网址
        private void ProcessUrl(WebUrl site)
        {
            SqlString cmd = new SqlString();
            string html;
            //删除重复数据
            cmd.Add("delete from [_pm_内容] where  uid='" + site.Uid + "'");
            if (Regex.IsMatch(site.Url, "\\.(pdf|doc|ppt)$"))
            {
                cmd.Add("insert into [_pm_内容]([uid],[type]) values('" + site.Uid + "','." + Regex.Match(site.Url, "(pdf|doc|ppt)$").Groups[1].Value + "')");
            }
            else if ((html = source.GetThreeTimes(site.Url)).Equals(""))
            {
                cmd.Add("update [_pm_网址] set times=times+1 where uid='" + site.Uid + "'");
                sql.Update(cmd.Read());
                SetNumAdd();
                return;
            }
            else
            {
                html = Regex.Replace(html, "[\0]", "");
                string title = Regex.Match(html, reg_title, RegexOptions.IgnoreCase).Groups[1].Value.Replace("'", "''").Trim();
                cmd.Add("insert into [_pm_内容]([uid],[title],[cont]) values('" + site.Uid + "','" + title + "','" + html.Replace("'", "''") + "')");
            }
            cmd.Add("update [_pm_网址] set times=-1 where uid='" + site.Uid + "'");
            try
            {
                sql.Update(cmd.Read());
            }
            catch (Exception e)
            {
                Display("错误:" + e.Message);
                sql.Update("update [_pm_网址] set times=times+1 where uid='" + site.Uid + "'");
            }
            SetNumAdd();
        }
    }
}
